<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>websocket</title>
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
		  integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
</head>
<body>
<div class="container">
	<div id="bg_div" style="background-color: orange;height: 20px;font-size: 18px"></div>
	<p></p>
	<button onclick="WebSocketTest()">Active</button>
</div>
</body>
<script type="text/javascript">
    function WebSocketTest() {
        let divNode = document.getElementById("bg_div");
        if ("WebSocket" in window) {
            let ws = new WebSocket("ws://127.0.0.1:8080/socketService")
            ws.onopen = function () {
                ws.send("hi , I am from client 007");
            };
            let i = 18;
            ws.onmessage = function (evt) {
                divNode.innerText += evt.data;
                divNode.style.width = i + "px";
                i = i + 18;
            };
            ws.onclose = function () {
                // 关闭 websocket
                alert("连接已关闭...");
            };
        } else {
            alert("你的浏览器不支持WebSocket !")
        }

    }
</script>
</html>